import { defineStore } from "pinia";
import { queryBigscreenResultBySqlCodeAndOrgId } from "src/utils/requestAction";

export const useBsOrgStore = defineStore("bsorg", {
  state: () => ({
    orgId: "",
    orgName: "",
    orgAbbr: "",
    orgLevel: "",
    childOrgs: [],
    parentOrgId: "",
  }),
  persist: {
    enabled: true,
  },
  getters: {
    getOrgId: (state) => state.orgId,
  },
  actions: {
    async setOrgByOrgId(orgId) {
      const res = await queryBigscreenResultBySqlCodeAndOrgId("org", orgId);
      const resChild = await queryBigscreenResultBySqlCodeAndOrgId(
        "childorg",
        orgId
      );
      if (res && res.data.records[0] && resChild && resChild.data.records[0]) {
        const org = res.data.records[0];
        org.child_orgs = resChild.data.records;
        this.orgId = org.org_id;
        this.orgName = org.org_name;
        this.orgAbbr = org.org_abbr;
        this.orgLevel = org.org_level;
        this.parentOrgId = org.parent_org_id;
        this.childOrgs = org.child_orgs;
      }
    },
  },
});
